package concurrency.chapter3;

import java.util.Arrays;

public class CreateThread2 {
    public static void main(String[] args) {
        //3、如果构造线程对象时未传入ThreadGroup，Thread会默认获取父线程的ThreadGroup作为该线程的ThreadGroup
        //此时子线程和父线程将会在同一个ThreadGroup中
        Thread t = new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        //System.out.println(t.getThreadGroup());
        //System.out.println(Thread.currentThread().getName());
        //System.out.println(Thread.currentThread().getThreadGroup().getName());

        ThreadGroup threadGroup=Thread.currentThread().getThreadGroup();
        System.out.println(threadGroup.activeCount());
        Thread[] threads=new Thread[threadGroup.activeCount()];
        //enumerate方法用来将ThreadGroup线程组中的active线程全部复制到Thread类型的数组中，并且返回数组中元素个数，即线程组中active线程数量。
        threadGroup.enumerate(threads);
        //for (Thread temp:threads){
        //    System.out.println(temp);
        //}
        Arrays.asList(threads).forEach(System.out::println);
        //3
        //Thread[main,5,main]
        //Thread[Monitor Ctrl-Break,5,main]
        //Thread[Thread-0,5,main]
    }
}
